package com.example.erhuo.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.erhuo.R;
import com.example.erhuo.api.NetAPI;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddWantActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText titleEdit;
    private EditText priceEdit;
    private Button commitButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.title_layout);
            TextView textView = (TextView) actionBar.getCustomView().findViewById(R.id.display_title);
            textView.setText("发布求购信息");
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
        }
        setContentView(R.layout.activity_add_want);
        titleEdit = findViewById(R.id.addWant_edit_title);
        priceEdit = findViewById(R.id.addWant_edit_price);
        commitButton = findViewById(R.id.addWant_button_commit);
        commitButton.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addWant_button_commit: {
                String title = titleEdit.getText().toString();
                String price = priceEdit.getText().toString();
                if (title.equals("") || price.equals("")) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(AddWantActivity.this);
                    builder.setTitle("不能为空!");
                    builder.setMessage("请填写全部商品信息");
                    builder.setPositiveButton("好的", null);
                    builder.show();
                    return;
                }
                RequestBody body = new FormBody.Builder()
                        .add("name",title)
                        .add("price",price)
                        .build();
                Callback callback = new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        Log.e("AddWant","添加失败");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "发布失败", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        String information = response.body().string();
                        Log.i("AddWant", information);
                        JSONObject json;
                        try {
                            json = new JSONObject(information);
                            String code = json.getString("code");
                            switch (code) {
                                case "200": {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(), "发布成功", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                    Log.i("AddWant","发布成功");
                                    finishThis();
                                    break;
                                }
                                default: {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getApplicationContext(), "发布失败", Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                NetAPI.postJsonWithToken("api/RequireOper/createRequire/",body,callback,MainActivity.token);
                break;
            }
        }
    }
    public void finishThis() {
        this.finish();
    }
}